
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="90px" class="demo-ruleForm">
          <el-form-item label="联系人:" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入联系人" />
          </el-form-item>
          <el-form-item label="区号:" prop="area_code">
            <el-select
              v-model="ruleForm.area_code"
              style="width:100%"
              placeholder="请选择区号"
              clearable
            >
              <el-option
                v-for="(item,index) in AreaCodeList"
                :key="index"
                :label="'+'+item.area_code+' ' + item.area_name"
                :value="item.area_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="电话:" prop="mobile">
            <el-input v-model="ruleForm.mobile" placeholder="请输入电话" />
          </el-form-item>
          <el-form-item label="邮编:" prop="postcode">
            <el-input v-model="ruleForm.postcode" placeholder="请输入邮编" />
          </el-form-item>
          <el-form-item v-if="showArea" label="选择区域" prop="nation">
            <el-cascader
              v-model="areaList"
              filterable
              :props="codes"
              placeholder="请选择区域"
              :options="linkList"
              style="width: 100%"
              @change="changeArea()"
            />
          </el-form-item>
          <el-form-item label="详细地址:" prop="detailed">
            <el-input v-model="ruleForm.detailed" type="textarea" placeholder="请输入详细地址" />
          </el-form-item>
          <el-form-item label="默认收货:" prop="is_shipments">
            <el-switch
              v-model="ruleForm.is_shipments"
              active-color="#1890ff"
              inactive-color="#ccc"
              :active-value="1"
              :inactive-value="2"
              active-text="是"
              inactive-text="否"
            />
          </el-form-item>
          <el-form-item label="默认退货:" prop="is_return">
            <el-switch
              v-model="ruleForm.is_return"
              active-color="#1890ff"
              inactive-color="#ccc"
              :active-value="1"
              :inactive-value="2"
              active-text="是"
              inactive-text="否"
            />
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createSystemSender, updateSystemSender } from '@/api/system/index'
import { mapState } from 'vuex'
import { countryAddressInfo } from '@/api/base/country'
import { listMobileAreaCode } from '@/api/user'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    },
    countryCodeList: {
      type: Array,
      default: () => {

      } }
  },
  data() {
    return {
      showDialog: false,
      ruleForm: {
        'name': '',
        'area_code': '',
        'mobile': '',
        'postcode': '',
        'detailed': '',
        'is_shipments': 2,
        'is_return': 2,
        'nation': '',
        'province': '',
        'city': '',
        'district': ''
      },
      codes: {
        label: 'name',
        value: 'name',
        children: '_child'
      },
      rules: {
        name: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        area_code: [
          { required: true, message: '请选择区号' }
        ],
        mobile: [
          { required: true, message: '请输入电话' }
        ],
        detailed: [
          { required: true, message: '请输入详细地址' }
        ],
        postcode: [
          { required: true, message: '请输入邮编' }
        ],
        nation: [
          { required: true, message: '请选择区域' }
        ]
      },
      AreaCodeList: [],
      areaList: [],
      showArea: false
    }
  },
  computed: {
    linkList: {
      get() { return this.$store.state.app.linkList },
      set(value) { }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
        this.showArea = false
        this.ruleForm = {
          'name': '',
          'area_code': '',
          'mobile': '',
          'postcode': '',
          'detailed': '',
          'is_shipments': 2,
          'is_return': 2,
          'nation': '',
          'province': '',
          'city': '',
          'district': ''
        }
      } else {
        this.areaList = []
        this.showArea = true
        if (this.status === 2) {
          Object.keys(this.ruleForm).forEach(i => {
            this.ruleForm[i] = this.detial[i]
          })
          this.ruleForm.id = this.detial.id
          this.areaList.push(this.ruleForm.nation, this.ruleForm.province, this.ruleForm.city, this.ruleForm.district)
        }
      }
    }
  },
  mounted() {
    this.getCountryAddressInfo()
    this.getListMobileAreaCode()
  },
  methods: {
    getCountryAddressInfo() {
      if (!this.linkList.length) {
        countryAddressInfo().then(res => {
          this.linkList = res.data
          this.$store.dispatch('app/setLink', res.data)
        })
      }
    },
    getListMobileAreaCode() {
      listMobileAreaCode().then(res => {
        this.AreaCodeList = res.data.list
      })
    },
    changeArea() {
      this.areaList.push('')
      this.ruleForm.nation = this.areaList[0]
      this.ruleForm.province = this.areaList[1]
      this.ruleForm.city = this.areaList[2]
      this.ruleForm.district = this.areaList[3]
    },
    close() {
      this.$emit('update', false)
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createSystemSender(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateSystemSender(this.ruleForm).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
